#!/usr/bin/env python
# encoding: utf-8

import pycurl, json, urllib, urllib2
auth_data = {"grant_type": "password", "username": "rachel_wang02@163.com", "password":"e10adc3949ba59abbe56e057f20f883e", "client_id":"123", "client_secret":"668e7c8391ff7ec8b98bb07b757583e08c8b1c3e"}
url_prefix='http://api-qa.jiankongbao.com'
url_suffix = '/v2/oauth/token.json'
url =url_prefix + url_suffix
def get_access_token(url, auth_data):
	try:
   	 	from io import BytesIO
   	except ImportError:
      	 from StringIO import StringIO as BytesIO
	header = BytesIO() 
	buffer = BytesIO()
	
	c = pycurl.Curl()
	c.setopt(c.URL, url)
	#c.setopt(c.VERBOSE, True)
	#c.setopt(c.POST, False)
	c.setopt(c.SSL_VERIFYPEER, False)
	c.setopt(c.POSTFIELDS, urllib.urlencode(auth_data))
	c.setopt(c.WRITEFUNCTION, buffer.write)
	c.setopt(c.HEADERFUNCTION, header.write)
	c.perform()
	result_str = buffer.getvalue()
	print result_str
#     print result_str
	#return json.loads(result_str)
	#print(json.loads(response.text)['access_token']), (auth_data)
	#print type(json.loads(response.text))

def main():
	get_access_token(url, auth_data)

if __name__ == '__main__':
	main()	

